﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyEngine.Entities;
using TinyGameSample.CustomEntities.Parsers;
using xTile.Display;

using xTile;
using xTileRectangle = xTile.Dimensions.Rectangle;
using TinyEngine.Core.Attributes;
using TinyEngine.Core.Entities;
using TinyEngine.Core.Scene;
using TinyEngine.Core.Services;
using Microsoft.Xna.Framework;
namespace ThirdPartyPlugins
{
    [EntityParser(typeof(CustomTileGridParser))]
    public class CustomTileGridEntity : NullEntity, IRenderable, IDebugRenderable, IUpdatable
    {
        public string Ref { get; set; }
        XnaDisplayDevice displayDevice;
        Map map;
        xTileRectangle viewPort;
        public CustomTileGridEntity()
        {
            
        }


        public void Draw(float elapsed)
        {
            this.map.Draw(displayDevice, this.viewPort);
        }

        public override void PositionChanged()
        {
            base.PositionChanged();
        }

        public override void Register(TinyScene owner)
        {
            base.Register(owner);
            var engine = ServiceManager.GetService<TinyGameEngine>();

            this.displayDevice = new XnaDisplayDevice(engine.Content, engine.Device);
            map = engine.Content.Load<Map>(this.Ref);
            map.LoadTileSheets(this.displayDevice);

            viewPort = new xTileRectangle(new xTile.Dimensions.Size(engine.Device.Viewport.Width, engine.Device.Viewport.Height));
            viewPort.Location = new xTile.Dimensions.Location(0, 240);
        }

        public Vector2 Origin { get; set; }

        public void Update(float elapsed)
        {
            
            map.Update((long)(elapsed * 1000));
        }
        public void DrawDebug(float elapsed)
        {
        
        }
    }
}
